using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using Granite.Modeling;
using Android.App;
using Android.Content;

namespace Granite.Axml
{
	/// <summary>
	/// This view model is closely linked to activities that inherit from NavigationActivity.
	/// </summary>
	public class NavigationViewModel : SimpleModelBase
	{
		/// <summary>
		/// Opens the indicated website in an external window.
		/// </summary>
		/// <param name="target"></param>
		public void OpenWebsite(string target)
		{
			if (target == null)
				throw new ArgumentNullException("target", "target is null.");
			if (String.IsNullOrEmpty(target))
				throw new ArgumentException("target is empty.", "target");
			Contract.EndContractBlock();

			OpenWebsite(Android.Net.Uri.Parse(target));
		}

		/// <summary>
		/// Opens the indicated website in an external window.
		/// </summary>
		/// <param name="target"></param>
		public void OpenWebsite(Uri target)
		{
			if (target == null)
				throw new ArgumentNullException("target", "target is null.");
			Contract.EndContractBlock();

			OpenWebsite(Android.Net.Uri.Parse(target.ToString()));
		}

		/// <summary>
		/// Opens the indicated website in an external window.
		/// </summary>
		/// <param name="target"></param>
		public void OpenWebsite(Android.Net.Uri target)
		{
			if (target == null)
				throw new ArgumentNullException("target", "target is null.");
			Contract.EndContractBlock();

			Navigate(new Intent(Intent.ActionView, target));
		}

		/// <summary>
		/// Navigates to the indicated activity type
		/// </summary>
		/// <param name="activityType">A type of Activity</param>
		public void Navigate(Type activityType)
		{
			if (activityType == null)
				throw new ArgumentNullException("activityType", "activityType is null.");
			if (!activityType.IsSubclassOf(typeof(Activity)))
				throw new ArgumentException("activityType is not a subtype of Activity", "activity");
			Contract.EndContractBlock();

			CurrentActivity.StartActivity(new Intent(CurrentActivity, activityType));
		}

		public void Navigate(Intent intent)
		{
			CurrentActivity.StartActivity(intent);
		}

		/// <summary>
		/// This represent the currently active Activity.
		/// </summary>
		/// <remarks>
		/// Activities that don't inherit from NavigationActivity should explicitly set this in the OnResume method.
		/// </remarks>
		public Activity CurrentActivity { get; set; }



	}
}
